package controller

import (
	"blog_server/global"
	"blog_server/internal/model/request"
	"blog_server/internal/service"
	"blog_server/pkg/app"
	"blog_server/pkg/errcode"
	"github.com/gin-gonic/gin"
	"strconv"
)

type TagController struct{}

func NewTagController() TagController {
	return TagController{}
}

func (t *TagController) Create(c *gin.Context) {
	param := request.TagRequest{}
	response := app.NewResponse(c)
	err := c.ShouldBind(&param)
	if err != nil {
		global.Logger.Errorf("c.ShouldBind errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	svc := service.New(c.Request.Context())
	err = svc.CreateTag(&param)
	if err != nil {
		global.Logger.Errorf("svc.CreateTag errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (t *TagController) GetArticleTagAmount(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	articleCnt, tagCnt, err := svc.GetArticleTagAmount()
	if err != nil {
		global.Logger.Errorf("svc.CreateTag errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"articleCnt": articleCnt, "tagCnt": tagCnt})
}

func (t *TagController) GetByPagination(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	pageSize, _ := app.GetPageSize(c)
	page, _ := app.GetPage(c)
	res, cnt, err := svc.GetTagsByPagination(page, pageSize)
	if err != nil {
		global.Logger.Errorf("svc.GetTagsByPagination err: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": res, "cnt": cnt})
}

func (t *TagController) Update(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	param := request.TagRequest{}
	res := c.Param("id")
	linkId, err := strconv.Atoi(res)
	if err != nil {
		global.Logger.Errorf("strconv.Atoi errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = c.ShouldBind(&param)
	if err != nil {
		global.Logger.Errorf("ShouldBind errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.UpdateTag(uint(linkId), &param)
	if err != nil {
		global.Logger.Errorf("svc.UpdateTag errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (t *TagController) Delete(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	res := c.Param("id")
	linkId, err := strconv.Atoi(res)
	if err != nil {
		global.Logger.Errorf("strconv.Atoi errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.DeleteTag(uint(linkId))
	if err != nil {
		global.Logger.Errorf("svc.DeleteTag errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (t *TagController) GetBySearchText(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	searchText := c.Param("search_text")
	res, err := svc.GetTagListBySearchText(searchText)
	if err != nil {
		global.Logger.Errorf("svc.GetTagListBySearchText errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": res})
}
